package com.sicheng.lc.杂题.二叉树;

import leetcode.editor.cn.tree.TreeNode;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/13 16:32
 */
public class 从根到叶的二进制数之和 {
    //https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/
    int res = 0;

    public int sumRootToLeaf(TreeNode root) {
        dfs(root, 0);
        return res;
    }

    public void dfs(TreeNode root, int x) {
        if (root.left == null && root.right == null) {
            res += x << 1 | root.val;
            return;
        }

        if (root.left != null)
            dfs(root.left, x << 1 | root.val);
        if (root.right != null)
            dfs(root.right, x << 1 | root.val);

    }
}
